from appium.webdriver.common.appiumby import AppiumBy
from base.base_page import BasePage  # 需根据实际项目导入 BasePage 所在模块


class QueryPage(BasePage):
    # 页面标题
    PAGE_TITLE = (AppiumBy.XPATH, "//android.widget.TextView[@text='查件']")
    # 运单号输入框
    WAYBILL_NO_INPUT = (AppiumBy.XPATH, "//android.widget.EditText[@hint='运单号']")
    # 搜索按钮（输入框右侧搜索图标，若有独立按钮可这样定位，也可输入后按回车）
    SEARCH_BUTTON = (AppiumBy.XPATH, "//android.widget.ImageView[@resource-id='...' and @content-desc='搜索']")
    # 扫码图标（输入框右侧扫码按钮）
    SCAN_CODE_ICON = (AppiumBy.XPATH, "//android.widget.ImageView[@resource-id='...' and @content-desc='扫码']")
    # 筛选按钮
    FILTER_BUTTON = (AppiumBy.XPATH, "//android.widget.TextView[@text='筛选']")
    # 更多按钮
    MORE_BUTTON = (AppiumBy.XPATH, "//android.widget.TextView[@text='更多']")
    # 我寄的 tab
    SENT_BY_ME_TAB = (AppiumBy.XPATH, "//android.widget.TextView[@text='我寄的']")
    # 我收的 tab
    RECEIVED_BY_ME_TAB = (AppiumBy.XPATH, "//android.widget.TextView[@text='我收的']")
    # 全部 tab
    ALL_TAB = (AppiumBy.XPATH, "//android.widget.TextView[@text='全部']")
    # 待揽件 tab
    PENDING_PICKUP_TAB = (AppiumBy.XPATH, "//android.widget.TextView[@text='待揽件']")
    # 待签收 tab
    PENDING_RECEIPT_TAB = (AppiumBy.XPATH, "//android.widget.TextView[@text='待签收']")
    # 已签收 tab
    RECEIVED_TAB = (AppiumBy.XPATH, "//android.widget.TextView[@text='已签收']")
    # 提示文本（默认展示近60天数据...）
    TIP_TEXT = (AppiumBy.XPATH, "//android.widget.TextView[@text='默认展示近60天数据，可筛选查询近1年内运单']")
    # 去寄件按钮
    GO_SHIPMENT_BUTTON = (AppiumBy.XPATH, "//android.widget.TextView[@text='去寄件']")
    # 底部导航 - 首页
    HOME_NAV = (AppiumBy.XPATH, "//android.widget.TextView[@text='首页']")
    # 底部导航 - 寄件
    SHIPMENT_NAV = (AppiumBy.XPATH, "//android.widget.TextView[@text='寄件']")
    # 底部导航 - 查件（当前选中态，可通过样式区分，若需精确定位可补充 resource-id 等）
    CHECK_PARCEL_NAV = (AppiumBy.XPATH, "//android.widget.TextView[@text='查件']")
    # 底部导航 - 我的
    MY_NAV = (AppiumBy.XPATH, "//android.widget.TextView[@text='我的']")
    CHECK_PAGE_TITLE = (AppiumBy.XPATH, "//android.widget.TextView[@text='查件']")  # 查件页标题


    # ---------------------- 操作方法 ----------------------

    def is_check_page_displayed(self):
        """判断是否跳转到查件页（根据标题判断）"""
        return self.is_element_exist(self.CHECK_PAGE_TITLE)

    def is_data_refreshed(self):
        """验证查件页数据是否刷新（示例：判断是否有运单加载或提示变化，需结合业务）"""
        # 无符合条件运单时显示“空空如也”，有数据时运单列表存在
        return self.is_element_exist((AppiumBy.XPATH, "//android.widget.TextView[@text='空空如也，去寄件吧']")) is False

    def input_waybill_no(self, waybill_no):
        """
        在运单号输入框输入运单号
        :param waybill_no: 运单号内容，如 "123456789"
        """
        self.send_keys(self.WAYBILL_NO_INPUT, waybill_no)

    def click_search_button(self):
        """点击搜索按钮（输入框右侧搜索图标）"""
        self.click(self.SEARCH_BUTTON)

    def click_scan_code_icon(self):
        """点击扫码图标（切换到扫码查件功能）"""
        self.click(self.SCAN_CODE_ICON)

    def click_filter_button(self):
        """点击筛选按钮，打开筛选条件弹窗"""
        self.click(self.FILTER_BUTTON)

    def click_more_button(self):
        """点击更多按钮，查看更多操作选项"""
        self.click(self.MORE_BUTTON)

    def switch_to_sent_by_me(self):
        """切换到“我寄的” tab"""
        self.click(self.SENT_BY_ME_TAB)

    def switch_to_received_by_me(self):
        """切换到“我收的” tab"""
        self.click(self.RECEIVED_BY_ME_TAB)

    def switch_to_all(self):
        """切换到“全部” tab（默认已选中，可用于断言或重置）"""
        self.click(self.ALL_TAB)

    def switch_to_pending_pickup(self):
        """切换到“待揽件” tab"""
        self.click(self.PENDING_PICKUP_TAB)

    def switch_to_pending_receipt(self):
        """切换到“待签收” tab"""
        self.click(self.PENDING_RECEIPT_TAB)

    def switch_to_received(self):
        """切换到“已签收” tab"""
        self.click(self.RECEIVED_TAB)

    def click_go_shipment(self):
        """点击“去寄件”按钮，跳转到寄件页面"""
        self.click(self.GO_SHIPMENT_BUTTON)

    def get_current_tab_text(self):
        """获取当前选中的标签文本"""
        # 标签选中后有特定样式（如字体加粗、颜色变化），通过定位选中态元素获取文本
        selected_tab = self.find_element((AppiumBy.XPATH, "//android.widget.TextView[@selected='true']"))
        return selected_tab.text

    def is_filter_page_displayed(self):
        """判断是否跳转到筛选页（根据筛选页标题判断）"""
        return self.is_element_exist((AppiumBy.XPATH, "//android.widget.TextView[@text='筛选']"))

    def is_empty_state_displayed(self):
        """判断是否显示无运单状态"""
        return self.is_element_exist((AppiumBy.XPATH, "//android.widget.TextView[@text='空空如也，去寄件吧']"))

    def is_shipment_page_displayed(self):
        """判断是否跳转到寄件页（根据寄件页标题判断）"""
        return self.is_element_exist((AppiumBy.XPATH, "//android.widget.TextView[@text='寄快递']"))

